package com.wan.controller;

import cn.hutool.core.date.DateUtil;
import com.wan.entity.GameAnnouncement;
import com.wan.service.GameAnnouncementService;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author xzx
 * @date 2021/4/13 9:52
 */
@Controller
@RequestMapping("/gonggao")
public class GongGaoController {
    @Autowired
    private GameAnnouncementService announcementService;
    @Value("${map.per}")
    private String per;



    @RequestMapping("/save")
    public ResultBean addGongGao(String title,String imageFile,String auth,String content,String introduction){
        GameAnnouncement gameAnnouncement = new GameAnnouncement();
        gameAnnouncement.setTitle(title);
        gameAnnouncement.setAuth(auth);
        gameAnnouncement.setIntroduction(introduction);
        String url = imageFile.substring(per.length());
        gameAnnouncement.setCover(url);
        gameAnnouncement.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        gameAnnouncement.setContent(content);
        announcementService.save(gameAnnouncement);
        return new ResultBean(200,"添加成功");
    }

    @RequestMapping("/update")
    public ResultBean editGongGao(String title,String imageFile,String auth,String content,String introduction,Integer id){
        GameAnnouncement announcement = announcementService.getById(id);
        if (imageFile.contains("http")){
            imageFile = imageFile.substring(per.length());
        }
        announcement.setTitle(title);
        announcement.setCover(imageFile);
        announcement.setAuth(auth);
        announcement.setContent(content);
        announcement.setIntroduction(introduction);
        announcementService.updateById(announcement);
        return new ResultBean(200,"更新成功");
    }
}
